#import "WebViewController.h"
#import "QuestetraBPMAppDelegate.h"

@implementation WebViewController

@synthesize url;

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
	NSDictionary *headers = [request allHTTPHeaderFields];
    BOOL hasWhateverAddedHeader = NO;
    for(NSString *key in [headers allKeys]) {
        if([[key lowercaseString] isEqualToString:@"authorization"]) {
            hasWhateverAddedHeader = YES;
            break;
        }
    }
    if(!hasWhateverAddedHeader) {
        NSMutableURLRequest *newRequest = [request mutableCopy];
		[[[QuestetraBPMAppDelegate sharedApp] qbpmClient] addAuthorizationHeader: newRequest];
        [(UIWebView*)self.view loadRequest:newRequest];
        [newRequest release];
        return NO;
    }
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

- (void)viewDidLoad {
	[(UIWebView*)self.view loadRequest: [NSURLRequest requestWithURL:url]];
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc {
    [super dealloc];
}


@end
